<template>
  <div class="login">
    <el-form class="box" :model="form" :rules="rules" ref="form">
      <div class="logo">
        <img src="@/assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="submit">登入</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {login} from '@/api/user'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',//手机号码
        password: '123456'//密码
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              const req = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (req.test(value)) {
                callback()
              } else {
                callback(new Error('请填写正确手机号'))
              }
            }
          }
        ],
        password: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '填写6到12位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          // const res = await login(this.form)
          // console.log(res)
          // this.$message.success('验证成功')
        await  this.$store.dispatch('user/isLogin',this.form)
        //接收参数并跳转页面
        const redirect=this.$route.query.redirect||'/'
        this.$message.success('登入成功')
        this.$router.push(redirect)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    .logo {
      text-align: center;
      padding-bottom: 20px;
      
    }
    .btn {
        width: 100%;
        padding: 15px;
        background-color: #407ffe;
        border: none;
      }
  }
}
</style>
